package com.service.thirddatacenter.chiaexplorer.service.impl;

import com.service.thirddatacenter.chiaexplorer.client.ChiaExplorerClient;
import com.service.thirddatacenter.chiaexplorer.client.rep.BlockchainSummary;
import com.service.thirddatacenter.chiaexplorer.service.ChiaExplorerService;
import com.service.thirddatacenter.hpool.enums.BlockchainEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.service.thirddatacenter.chiaexplorer.mapper.BlockChainRecordMapper;
import com.service.thirddatacenter.chiaexplorer.model.entity.BlockChainRecordEntity;
import com.service.thirddatacenter.chiaexplorer.service.BlockChainRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 区块链总计
 *
 * @date 2021-06-08 15:41:03
 */
@Slf4j
@Service("blockChainRecordService")
public class BlockChainRecordServiceImpl extends ServiceImpl<BlockChainRecordMapper, BlockChainRecordEntity> implements BlockChainRecordService {

    @Autowired
    private ChiaExplorerClient chiaExplorerClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createBlockChain() {

        try {
            BlockchainSummary blockchainSummary = chiaExplorerClient.getBlockchainSummary();
            save(BlockChainRecordEntity.builder()
                    .type(BlockchainEnum.CHIA.getName())
                    .blockChainId(blockchainSummary.getId())
                    .addressCount(blockchainSummary.getAddressCount())
                    .averageFees(blockchainSummary.getAverageFees())
                    .difficulty(blockchainSummary.getDifficulty())
                    .netspace(blockchainSummary.getNetspace())
                    .power(blockchainSummary.getPower())
                    .supply(blockchainSummary.getSupply())
                    .price(blockchainSummary.getPrice())
                    .uniqueCoins(blockchainSummary.getUniqueCoins())
                    .createTime(new Date())
                    .build());

        }catch (Exception e){
            log.error("获取区块总计失败：{}", e.getMessage());
        }

    }
}
